package com.chuisha.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuisha.stu.service.ProductService;
import com.chuisha.stu.service.dao.ProductDao;
import com.chuisha.stu.service.dao.ProductEntity;
import com.chuisha.stu.service.vo.ProductVO;
import com.chuisha.stu.service.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;

    public ProductVO selectByCode(String productCode) {
        ProductEntity entity = new ProductEntity();
        entity.setProductCode(productCode);
        entity = productDao.selectOne(new QueryWrapper<>(entity));
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(entity, productVO);
        return productVO;
    }

    public ProductVO updateProduct(ProductVO productVO) {
        ProductEntity entity = new ProductEntity();
        BeanUtils.copyProperties(productVO, entity);
        productDao.updateById(entity);
        return new ProductVO();
    }

    public ProductVO insertProduct(ProductVO productVO) {

        ProductEntity entity = new ProductEntity();
        BeanUtils.copyProperties(productVO, entity);
        productDao.insert(entity);
        productVO.setId(entity.getId());
        return productVO;
    }

    public int deleteProduct(String productCode) {
        ProductEntity entity = new ProductEntity();
        entity.setProductCode(productCode);
        return productDao.delete(new QueryWrapper<>(entity));
    }
}
